<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>BodyWriter</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Beast">
<link rel="up" href="../concepts.html" title="Concepts">
<link rel="prev" href="BodyReader.html" title="BodyReader">
<link rel="next" href="BufferSequence.html" title="BufferSequence">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="BodyReader.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../concepts.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="BufferSequence.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="beast.concepts.BodyWriter"></a><a class="link" href="BodyWriter.html" title="BodyWriter">BodyWriter</a>
</h3></div></div></div>
<p>
        A <span class="bold"><strong>BodyWriter</strong></span> provides an <a href="https://en.wikipedia.org/wiki/Online_algorithm" target="_top">online
        algorithm</a> to obtain a sequence of zero or more buffers from a body
        during serialization. The implementation creates an instance of this type
        when needed, and calls into it one or more times to retrieve buffers holding
        body octets. The interface of <span class="bold"><strong>BodyWriter</strong></span>
        is intended to obtain buffers for these scenarios:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            A body that does not entirely fit in memory.
          </li>
<li class="listitem">
            A body produced incrementally from coroutine output.
          </li>
<li class="listitem">
            A body represented by zero or more buffers already in memory.
          </li>
<li class="listitem">
            A body whose size is not known ahead of time.
          </li>
<li class="listitem">
            Body data generated dynamically from other threads.
          </li>
<li class="listitem">
            Body data computed algorithmically.
          </li>
</ul></div>
<h5>
<a name="beast.concepts.BodyWriter.h0"></a>
        <span class="phrase"><a name="beast.concepts.BodyWriter.associated_types"></a></span><a class="link" href="BodyWriter.html#beast.concepts.BodyWriter.associated_types">Associated
        Types</a>
      </h5>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            <a class="link" href="../ref/boost__beast__http__is_body_writer.html" title="http::is_body_writer"><code class="computeroutput"><span class="identifier">is_body_writer</span></code></a>
          </li>
<li class="listitem">
            <a class="link" href="Body.html" title="Body"><span class="emphasis"><em>Body</em></span></a>
          </li>
</ul></div>
<h5>
<a name="beast.concepts.BodyWriter.h1"></a>
        <span class="phrase"><a name="beast.concepts.BodyWriter.requirements"></a></span><a class="link" href="BodyWriter.html#beast.concepts.BodyWriter.requirements">Requirements</a>
      </h5>
<div class="warning"><table border="0" summary="Warning">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../../../../doc/src/images/warning.png"></td>
<th align="left">Warning</th>
</tr>
<tr><td align="left" valign="top"><p>
          These requirements may undergo non-backward compatible changes in subsequent
          versions.
        </p></td></tr>
</table></div>
<p>
        In this table:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            <code class="computeroutput"><span class="identifier">W</span></code> denotes a type meeting
            the requirements of <span class="bold"><strong>BodyWriter</strong></span>.
          </li>
<li class="listitem">
            <code class="computeroutput"><span class="identifier">B</span></code> denotes a <a class="link" href="Body.html" title="Body"><span class="emphasis"><em>Body</em></span></a>
            where <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">is_same</span><span class="special">&lt;</span><span class="identifier">W</span><span class="special">,</span> <span class="identifier">B</span><span class="special">::</span><span class="identifier">writer</span><span class="special">&gt;::</span><span class="identifier">value</span> <span class="special">==</span>
            <span class="keyword">true</span></code>.
          </li>
<li class="listitem">
            <code class="computeroutput"><span class="identifier">a</span></code> denotes a value of
            type <code class="computeroutput"><span class="identifier">W</span></code>.
          </li>
<li class="listitem">
            <code class="computeroutput"><span class="identifier">h</span></code> denotes a const value
            of type <code class="computeroutput"><span class="identifier">header</span><span class="special">&lt;</span><span class="identifier">isRequest</span><span class="special">,</span>
            <span class="identifier">Fields</span><span class="special">&gt;</span>
            <span class="keyword">const</span><span class="special">&amp;</span></code>.
          </li>
<li class="listitem">
            <code class="computeroutput"><span class="identifier">v</span></code> denotes a possibly
            const value of type <code class="computeroutput"><span class="identifier">Body</span><span class="special">::</span><span class="identifier">value_type</span><span class="special">&amp;</span></code>.
          </li>
<li class="listitem">
            <code class="computeroutput"><span class="identifier">ec</span></code> is a value of type
            <a class="link" href="../ref/boost__beast__error_code.html" title="error_code"><code class="computeroutput"><span class="identifier">error_code</span><span class="special">&amp;</span></code></a>.
          </li>
<li class="listitem">
            <code class="computeroutput"><span class="identifier">W</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
            is the type <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="keyword">bool</span><span class="special">&gt;&gt;</span></code>.
          </li>
</ul></div>
<div class="table">
<a name="beast.concepts.BodyWriter.valid_expressions"></a><p class="title"><b>Table 1.39. Valid expressions</b></p>
<div class="table-contents"><table class="table" summary="Valid expressions">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Expression
                </p>
              </th>
<th>
                <p>
                  Type
                </p>
              </th>
<th>
                <p>
                  Semantics, Pre/Post-conditions
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">W</span><span class="special">::</span><span class="identifier">const_buffers_type</span></code>
                </p>
              </td>
<td>
              </td>
<td>
                <p>
                  A type which meets the requirements of <a href="../../../../../../doc/html/boost_asio/reference/ConstBufferSequence.html" target="_top"><span class="emphasis"><em>ConstBufferSequence</em></span></a>.
                  This is the type of buffer returned by <code class="computeroutput"><span class="identifier">W</span><span class="special">::</span><span class="identifier">get</span></code>.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">W</span><span class="special">{</span><span class="identifier">h</span><span class="special">,</span><span class="identifier">v</span><span class="special">};</span></code>
                </p>
              </td>
<td>
              </td>
<td>
                <p>
                  Constructible from <code class="computeroutput"><span class="identifier">h</span></code>
                  and <code class="computeroutput"><span class="identifier">v</span></code>. The lifetime
                  of <code class="computeroutput"><span class="identifier">h</span></code> and <code class="computeroutput"><span class="identifier">v</span></code> are guaranteed to end no earlier
                  than after the <code class="computeroutput"><span class="identifier">W</span></code>
                  is destroyed. The writer shall not access the contents of <code class="computeroutput"><span class="identifier">h</span></code> or <code class="computeroutput"><span class="identifier">v</span></code>
                  before the first call to <code class="computeroutput"><span class="identifier">init</span></code>,
                  permitting lazy construction of the message.
                </p>
                <p>
                  The constructor may optionally require that <code class="computeroutput"><span class="identifier">h</span></code>
                  and <code class="computeroutput"><span class="identifier">v</span></code> are <code class="computeroutput"><span class="keyword">const</span></code> references, with these consequences:
                </p>
                <p>
                  * If <code class="computeroutput"><span class="identifier">W</span></code> requires
                  that <code class="computeroutput"><span class="identifier">h</span></code> and <code class="computeroutput"><span class="identifier">v</span></code> are const references, then
                  the corresponding serializer constructors for messages with this
                  body type will will accept a const reference to a message, otherwise:
                </p>
                <p>
                  * If <code class="computeroutput"><span class="identifier">W</span></code> requires
                  that <code class="computeroutput"><span class="identifier">h</span></code> and <code class="computeroutput"><span class="identifier">v</span></code> are non-const references, then
                  the corresponding serializer constructors for messages with this
                  body type will require a non-const reference to a message.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">a</span><span class="special">.</span><span class="identifier">init</span><span class="special">(</span><span class="identifier">ec</span><span class="special">)</span></code>
                </p>
              </td>
<td>
              </td>
<td>
                <p>
                  Called once to fully initialize the object before any calls to
                  <code class="computeroutput"><span class="identifier">get</span></code>. The message
                  body becomes valid before entering this function, and remains valid
                  until the writer is destroyed. The function will ensure that <code class="computeroutput"><span class="special">!</span><span class="identifier">ec</span></code>
                  is <code class="computeroutput"><span class="keyword">true</span></code> if there was
                  no error or set to the appropriate error code if there was one.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">a</span><span class="special">.</span><span class="identifier">get</span><span class="special">(</span><span class="identifier">ec</span><span class="special">)</span></code>
                </p>
              </td>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">W</span><span class="special">&lt;</span><span class="identifier">W</span><span class="special">::</span><span class="identifier">const_buffers_type</span><span class="special">&gt;</span></code>
                </p>
              </td>
<td>
                <p>
                  Called one or more times after <code class="computeroutput"><span class="identifier">init</span></code>
                  succeeds. This function returns <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">none</span></code>
                  if all buffers representing the body have been returned in previous
                  calls or if it sets <code class="computeroutput"><span class="identifier">ec</span></code>
                  to indicate an error. Otherwise, if there are buffers remaining
                  the function should return a pair with the first element containing
                  a non-zero length buffer sequence representing the next set of
                  octets in the body, while the second element is a <code class="computeroutput"><span class="keyword">bool</span></code> meaning <code class="computeroutput"><span class="keyword">true</span></code>
                  if there may be additional buffers returned on a subsequent call,
                  or <code class="computeroutput"><span class="keyword">false</span></code> if the buffer
                  returned on this call is the last buffer representing the body.
                  The function will ensure that <code class="computeroutput"><span class="special">!</span><span class="identifier">ec</span></code> is <code class="computeroutput"><span class="keyword">true</span></code>
                  if there was no error or set to the appropriate error code if there
                  was one.
                </p>
              </td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><h5>
<a name="beast.concepts.BodyWriter.h2"></a>
        <span class="phrase"><a name="beast.concepts.BodyWriter.exemplar"></a></span><a class="link" href="BodyWriter.html#beast.concepts.BodyWriter.exemplar">Exemplar</a>
      </h5>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">BodyWriter</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">/// The type of buffer returned by `get`.</span>
    <span class="keyword">using</span> <span class="identifier">const_buffers_type</span> <span class="special">=</span> <span class="identifier">net</span><span class="special">::</span><span class="identifier">const_buffer</span><span class="special">;</span>

    <span class="comment">/** Construct the writer.

        @param h The header for the message being serialized

        @param body The body being serialized
    */</span>
    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">bool</span> <span class="identifier">isRequest</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Fields</span><span class="special">&gt;</span>
    <span class="identifier">BodyWriter</span><span class="special">(</span><span class="identifier">header</span><span class="special">&lt;</span><span class="identifier">isRequest</span><span class="special">,</span> <span class="identifier">Fields</span><span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">h</span><span class="special">,</span> <span class="identifier">value_type</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">body</span><span class="special">);</span>

    <span class="comment">/** Initialize the writer.

        This is called after construction and before the first
        call to `get`. The message is valid and complete upon
        entry.

        @param ec Set to the error, if any occurred.
    */</span>
    <span class="keyword">void</span>
    <span class="identifier">init</span><span class="special">(</span><span class="identifier">error_code</span><span class="special">&amp;</span> <span class="identifier">ec</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// The specification requires this to indicate "no error"</span>
        <span class="identifier">ec</span> <span class="special">=</span> <span class="special">{};</span>
    <span class="special">}</span>

    <span class="comment">/** Returns the next buffer in the body.

        @li If the return value is `boost::none` (unseated optional) and
            `ec` does not contain an error, this indicates the end of the
            body, no more buffers are present.

        @li If the optional contains a value, the first element of the
            pair represents a &lt;em&gt;ConstBufferSequence&lt;/em&gt; containing one or
            more octets of the body data. The second element indicates
            whether or not there are additional octets of body data.
            A value of `true` means there is more data, and that the
            implementation will perform a subsequent call to `get`.
            A value of `false` means there is no more body data.

        @li If `ec` contains an error code, the return value is ignored.

        @param ec Set to the error, if any occurred.
    */</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">const_buffers_type</span><span class="special">,</span> <span class="keyword">bool</span><span class="special">&gt;&gt;</span>
    <span class="identifier">get</span><span class="special">(</span><span class="identifier">error_code</span><span class="special">&amp;</span> <span class="identifier">ec</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// The specification requires this to indicate "no error"</span>
        <span class="identifier">ec</span> <span class="special">=</span> <span class="special">{};</span>

        <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">none</span><span class="special">;</span> <span class="comment">// for exposition only</span>
    <span class="special">}</span>
<span class="special">};</span>
</pre>
<h5>
<a name="beast.concepts.BodyWriter.h3"></a>
        <span class="phrase"><a name="beast.concepts.BodyWriter.models"></a></span><a class="link" href="BodyWriter.html#beast.concepts.BodyWriter.models">Models</a>
      </h5>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            <a class="link" href="../ref/boost__beast__http__basic_dynamic_body/writer.html" title="http::basic_dynamic_body::writer"><code class="computeroutput"><span class="identifier">basic_dynamic_body</span><span class="special">::</span><span class="identifier">writer</span></code></a>
          </li>
<li class="listitem">
            <a class="link" href="../ref/boost__beast__http__basic_file_body__writer.html" title="http::basic_file_body::writer"><code class="computeroutput"><span class="identifier">basic_file_body</span><span class="special">::</span><span class="identifier">writer</span></code></a>
          </li>
<li class="listitem">
            <a class="link" href="../ref/boost__beast__http__basic_string_body/writer.html" title="http::basic_string_body::writer"><code class="computeroutput"><span class="identifier">basic_string_body</span><span class="special">::</span><span class="identifier">writer</span></code></a>
          </li>
<li class="listitem">
            <a class="link" href="../ref/boost__beast__http__buffer_body/writer.html" title="http::buffer_body::writer"><code class="computeroutput"><span class="identifier">buffer_body</span><span class="special">::</span><span class="identifier">writer</span></code></a>
          </li>
<li class="listitem">
            <a class="link" href="../ref/boost__beast__http__empty_body/writer.html" title="http::empty_body::writer"><code class="computeroutput"><span class="identifier">empty_body</span><span class="special">::</span><span class="identifier">writer</span></code></a>
          </li>
<li class="listitem">
            <a class="link" href="../ref/boost__beast__http__span_body/writer.html" title="http::span_body::writer"><code class="computeroutput"><span class="identifier">span_body</span><span class="special">::</span><span class="identifier">writer</span></code></a>
          </li>
<li class="listitem">
            <a class="link" href="../ref/boost__beast__http__vector_body/writer.html" title="http::vector_body::writer"><code class="computeroutput"><span class="identifier">vector_body</span><span class="special">::</span><span class="identifier">writer</span></code></a>
          </li>
</ul></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie
      Falco<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="BodyReader.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../concepts.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="BufferSequence.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
